// pages/nearby_group2/nearby_group2.js
var common = require("../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        color_header: "#fff",
        color_text: "white",
        color_main: "",
        color_minor: "",
        address: "",
        group: [],
        is_leader: 1,
        showInfoTip: false,
        //授权失败的弹框
        now_address: "",
        businessListFlag: true
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function() {
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        this.getBase();
        this.onChangeAddress();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarTitle({
                    title: "附近" + res.extra.leader_title ? res.extra.leader_title : "团长"
                });
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                wx.setNavigationBarTitle({
                    title: "附近" + (res.extra.leader_title ? res.extra.leader_title : "团长")
                });
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    leader_address_on: res.extra.leader_address_on,
                    communityName: res.extra.community_title ? res.extra.community_title : "社区"
                });
            }
        });
    },
    //弹出选择地址
    onChangeAddress: function() {
        var that = this;
        wx.chooseLocation({
            success: function(res) {
                that.setData({
                    now_address: res.name,
                    address: res.latitude + "," + res.longitude
                });
                that.getData();
            },
            fail: function(err) {
                that.continueGetSet();
            }
        });
    },
    //获取是否授权，是否需要提示
    continueGetSet: function() {
        let that = this;
        wx.getSetting({
            success(res) {
                if (res.authSetting["scope.userLocation"] == false) {
                    that.setData({
                        showInfoTip: true
                    });
                }
                if (res.authSetting["scope.userLocation"] == true) {
                    that.setData({
                        showInfoTip: false
                    });
                    //that.onLoad();
                                }
            }
        });
    },
    getData: function() {
        this.setData({
            businessListFlag: true
        });
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        var address = that.data.address;
        common.request({
            endpoint: "leader/near_leader",
            method: "post",
            data: {
                new_city_style: 0,
                //0 全部 1只搜索当前uniacid
                address: address,
                openid: openid
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                that.setData({
                    group: res.extra,
                    businessListFlag: false
                });
            },
            fail: res => {
                that.setData({
                    businessListFlag: false
                });
            }
        });
    },
    /** 
   * 设置团长信息
   */
    setLeaderInfo: function(e) {
        let that = this;
        var id = e.currentTarget.dataset.id;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "member/save_leader_id",
            method: "post",
            data: {
                openid: openid,
                leader_id: id
            },
            success: res1 => {
                wx.setStorage({
                    key: "leaderRefresh",
                    data: 1,
                    success(res) {
                        wx.navigateBack({
                            delta: 1,
                            success: function(e) {
                                var pages = getCurrentPages();
                                let prevPage = pages[pages.length - 2];
                                if (prevPage == undefined || prevPage == null) return;
                                if (res1.extra != "") {
                                    wx.setStorageSync("uniacid", res1.extra);
                                }
                                if (prevPage.route == "pages/pro_detail/pro_detail") {
                                    var options = {};
                                    options.id = prevPage.data.goodsId;
                                    prevPage.onLoad(options);
                                }
                                if (prevPage.route == "/newPlace_orderpages/newPlace_order/newPlace_order") {
                                    prevPage.getMyLeaderInfo(prevPage.data.all_price);
                                }
                            }
                        });
                    }
                });
            },
            fail(err) {
                if (err.code == 1) {
                    wx.showToast({
                        title: "你是" + that.data.leaderName + ",无法切换" + that.data.leaderName + "!",
                        icon: "none"
                    });
                } else {
                    wx.showToast({
                        title: err.message,
                        icon: "none"
                    });
                }
            }
        });
    }
});